<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #con{
            border: 2px solid pink; 
            width: 400px; 
            height: 550px; 
            padding: 30px;
        } 
        .bg{
            background-color: honeydew;
        }
    </style>
</head>
<body>
    <div id="con" class="list">
        <img src="001.jpg" style="width: 100%;"/>
    </div>
</body>
<script>
    var con = document.getElementsByClassName("list");//得到的是一个数组
    con[0].onmouseover = function(){
        //this.className = "bg";//修改class属性值为bg
        //this.className = this.className + " bg";//两个class属性值都存在
        
        var classStr = this.className;
        var classArr = classStr.split(" ");
        //添加bg
        if(classArr.indexOf("bg") >= 0){
            return;
        }else{
            this.className = this.className + " bg";
        }   
    }
    con[0].onmouseout = function(){
        //先得到一个数组
        var classStr = this.getAttribute("class");//标签中的属性
        var classArr = classStr.split(" ");
        //查找bg
        var index = classArr.indexOf("bg");
        if(index >= 0){
            //删除bg
            classArr.splice(index,1);
            var str = classArr.join(" ");
            this.setAttribute("class", str);
        }
        else{
            return;//函数没有return结果：undefined
        }
    }
    
</script>
</html>